package com.lxz.datasource.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.Optional;

public abstract class BaseServiceImpl<M extends JpaRepository<T, ID>, T, ID> implements BaseService<T, ID> {
	
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	protected M dao;
	@PersistenceContext
	protected EntityManager entityManager;

	@Override
	public Page<T> findAll(Pageable pageable) {
		return dao.findAll(pageable);
	}

	@Override
	public <S extends T> S save(S entity) {
		return dao.save(entity);
	}

	@Override
	public T findById(ID id) {
		Optional<T> optional = dao.findById(id);
		if (optional.isPresent()) {
			return optional.get();
		} else {
			return null;
		}
	}

	@Override
	public boolean existsById(ID id) {
		return dao.existsById(id);
	}

	@Override
	public long count() {
		return dao.count();
	}

	@Override
	public void deleteById(ID id) {
		dao.deleteById(id);
	}

	@Override
	public void delete(T entity) {
		dao.delete(entity);
	}

	@Override
	public void deleteAll(Iterable<? extends T> entities) {
		dao.deleteAll(entities);
	}

	@Override
	public void deleteAll() {
		dao.deleteAll();
	}

	@Override
	public <S extends T> Optional<S> findOne(Example<S> example) {
		return dao.findOne(example);
	}

	@Override
	public <S extends T> Page<S> findAll(Example<S> example, Pageable pageable) {
		return dao.findAll(example, pageable);
	}

	@Override
	public <S extends T> long count(Example<S> example) {
		return dao.count(example);
	}

	@Override
	public <S extends T> boolean exists(Example<S> example) {
		return dao.exists(example);
	}

	@Override
	public List<T> findAll() {
		return dao.findAll();
	}

	@Override
	public List<T> findAll(Sort sort) {
		return dao.findAll(sort);
	}

	@Override
	public List<T> findAllById(Iterable<ID> ids) {
		return dao.findAllById(ids);
	}

	@Override
	public <S extends T> List<S> saveAll(Iterable<S> entities) {
		return dao.saveAll(entities);
	}

	@Override
	public void flush() {
		dao.flush();
	}

	@Override
	public <S extends T> S saveAndFlush(S entity) {
		return dao.saveAndFlush(entity);
	}

	@Override
	public void deleteInBatch(Iterable<T> entities) {
		dao.deleteInBatch(entities);
	}

	@Override
	public void deleteAllInBatch() {
		dao.deleteAllInBatch();
	}

	@Override
	public <S extends T> List<S> findAll(Example<S> example) {
		return dao.findAll(example);
	}

	@Override
	public <S extends T> List<S> findAll(Example<S> example, Sort sort) {
		return dao.findAll(example, sort);
	}

}